//
// Copyright (C) 
// 
// File: GoCrowdNode.cpp
//
// Dependency Graph Node: GoCrowdNode
//
// Author: Maya Plug-in Wizard 2.0
//

#include "GoCrowdNode.h"

#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MGlobal.h>
#include <maya/MFnUnitAttribute.h>
#include <maya/MAngle.h>

// You MUST change this to a unique value!!!  The id is a 32bit value used
// to identify this type of node in the binary file format.  
//
// #error change the following to a unique value and then erase this line 
MTypeId     GoCrowdNode::id( 0x0cccc );

// Example attributes
// 
// MObject     GoCrowdNode::input;        
// MObject     GoCrowdNode::output;       

MObject     GoCrowdNode::distance;        
MObject     GoCrowdNode::radius;
MObject     GoCrowdNode::rotation;     

GoCrowdNode::GoCrowdNode() {}
GoCrowdNode::~GoCrowdNode() {}

MStatus GoCrowdNode::compute( const MPlug& plug, MDataBlock& data )
//
//	Description:
//		This method computes the value of the given output plug based
//		on the values of the input attributes.
//
//	Arguments:
//		plug - the plug to compute
//		data - object that provides access to the attributes for this node
//
{
	MStatus stat;

	if( plug == rotation )
	{
		MDataHandle distData = data.inputValue( distance, &stat );
		MDataHandle radData = data.inputValue( radius, &stat );

		double dist = distData.asDouble();
		double rad = radData.asDouble();

		MDataHandle rotData = data.outputValue( rotation, &stat );
		rotData.set( -dist / rad );

		data.setClean( plug );
	} 
	else 
		stat  = MS::kUnknownParameter;

	return stat;
}

void* GoCrowdNode::creator()
//
//	Description:
//		this method exists to give Maya a way to create new objects
//      of this type. 
//
//	Return Value:
//		a new object of this type
//
{
	return new GoCrowdNode();
}

MStatus GoCrowdNode::initialize()
//
//	Description:
//		This method is called to create and initialize all of the attributes
//      and attribute dependencies for this node type.  This is only called 
//		once when the node type is registered with Maya.
//
//	Return Values:
//		MS::kSuccess
//		MS::kFailure
//		
{
	MFnNumericAttribute nAttr;

	distance = nAttr.create( "distance", "dist", MFnNumericData::kDouble, 0.0 );
	/*
	nAttr.setStorable(true);
	nAttr.setKeyable(true);
	*/

	radius = nAttr.create( "radius", "rad", MFnNumericData::kDouble, 0.0 );
	/*
	nAttr.setStorable(true);
	nAttr.setKeyable(true);
	*/

	MFnUnitAttribute uAttr;
	rotation = uAttr.create( "rotation", "rot", MFnUnitAttribute::kAngle, 0.0 );
	/*
	uAttr.setWritable(false);
	uAttr.setStorable(false);
	*/

	addAttribute( distance );
	addAttribute( radius );
	addAttribute( rotation );

	attributeAffects( distance, rotation );
	attributeAffects( radius, rotation );

	return MS::kSuccess;
}
